﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Xml.Schema;
using SHARED;

namespace XML
{
    public class Output
    {
        public String filename;

        public Output(String filename)
        {
            this.filename = filename;
        }

        public void toXSD(XElement input)
        {
            XmlReader reader = input.CreateReader();

            XmlSchemaSet schemaSet = new XmlSchemaSet();
            XmlSchemaInference schema = new XmlSchemaInference();

            schemaSet = schema.InferSchema(reader);
            foreach (XmlSchema s in schemaSet.Schemas())
            {
                using (TextWriter writer = File.CreateText(filename + ".xsd"))
                {
                    s.Write(writer);
                }
            }
            XDocument d = new XDocument();

            XElement e = new XElement("Query");
            XAttribute a = new XAttribute(XName.Get("SchemaLocation", "http://www.w3.org/2001/XMLSchema-instance"), filename + ".xsd");

            d.Add(e);
            e.Add(a);

            XDocument finaldoc = new XDocument();
            finaldoc.Add(e);
            finaldoc.Root.Add(input);
            finaldoc.Save(filename);
        } //end toXSD

        public void toDTD(MasterElementList masterlist, XElement input)
        {
            String dtd = writeDTDString(masterlist);
            XDocument finaldoc = new XDocument(
            new XDocumentType("Result", null, null, dtd),
            new XElement(input));
            finaldoc.Save(filename);
        } //end toDTD

        //create dtd string
        public String writeDTDString(MasterElementList masterlist)
        {
            int level = 1;

            String output = "";
            for (int i = 0; i < masterlist.Count; i++)
            {
                if (masterlist[i].level < level)
                    break;
                else
                    level = masterlist[i].level;
                for (int p = 0; p < masterlist[i].elements.Count; p++)
                {

                    if (masterlist[i].elements[p].isTagOnly == true)
                    {
                        output += "<!ELEMENT " + masterlist[i].elements[p].tagName.ToString() + " (";
                    }
                    else
                    {
                        output += masterlist[i].elements[p].tagName.ToString();
                        if (p != masterlist[i].elements.Count - 1)
                            output += ",";
                    }


                }
                output += ")>\n";

            }

            //reset level
            level = 1;
            for (int i = 0; i < masterlist.Count; i++)
            {
                if (masterlist[i].level < level)
                    break;
                else
                    level = masterlist[i].level;

                for (int p = 0; p < masterlist[i].elements.Count; p++)
                {

                    if (masterlist[i].elements[p].isTagOnly != true)
                    {

                        output += "<!ELEMENT " + masterlist[i].elements[p].tagName.ToString() + " (#PCDATA)>\n";
                    }


                }

            }
            return output;
        }//end writeDTDtoString

    }
}
